﻿using System;
using System.Web.Http;
using SmartSSO.Entities;
using SmartSSO.Services;
using SmartSSO.Services.Impl;
using SmartSSO.Helpers;
using Microsoft.Practices.Unity;

namespace SmartSSO.ApiControllers
{
    /// <summary>
    /// WebApi控制器，用于验证用户是否已经登录
    /// </summary>
    public class PassportController : ApiController
    {
        //private readonly IUserSessionService _userSessionService = new UserSessionService();
        //private readonly IUserOperateService _userOperateService = new UserOperateService();
        private readonly IUserSessionService _userSessionService = UnityHelper.Instance.Unity.Resolve<IUserSessionService>();
        private readonly IUserOperateService _userOperateService = UnityHelper.Instance.Unity.Resolve<IUserOperateService>();

        /// <summary>
        /// 根据缓存验证用户是否已登录并记录日志
        /// </summary>
        /// <param name="sessionKey"></param>
        /// <param name="remark"></param>
        /// <returns></returns>
        public bool Get(string sessionKey = "", string remark = "")
        {
            if (_userSessionService.GetCache(sessionKey))
            {
                _userOperateService.Create(new UserOperateEntity
                {
                    CreateTime = DateTime.Now,
                    IpAddress = Request.RequestUri.Host,
                    Remark = string.Format("验证成功-{0}", remark),
                    SessionKey = sessionKey
                });
                return true;
            }

            _userOperateService.Create(new UserOperateEntity
            {
                CreateTime = DateTime.Now,
                IpAddress = Request.RequestUri.Host,
                Remark = string.Format("验证失败-{0}", remark),
                SessionKey = sessionKey
            });
            return false;
        }

    
    }
}